package com.example.anydo.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.anydo.R;
import com.example.anydo.bean.UserBean;
import com.example.anydo.service.AlarmReceiver;
import com.example.anydo.service.TimeTaskService;
import com.example.anydo.util.database.DatabaseHelper;
import com.example.anydo.util.pub_util.SPUtil;
import com.example.anydo.util.pub_util.Util;

import java.util.Calendar;

public class PersonInfoActivity extends AppCompatActivity {

    private TextView tv_date,tv_name;
    private TextView dayTask,historyTask,finshedTask,waitedTask,date;
    private ProgressBar dayTaskBar,historyTaskBar;
    private Button changePsw,logout;
    private ImageView cancel;


    private String userId;
    private DatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_info);


        dayTask = (TextView)findViewById(R.id.dayTask);//今日任务完成情况
        historyTask = (TextView)findViewById(R.id.historyTask);//历史任务完成情况
        finshedTask = (TextView)findViewById(R.id.finshedTask);//已经完成任务数
        waitedTask = (TextView)findViewById(R.id.waitedTask);//带规划任务
        date = (TextView)findViewById(R.id.date);//日期
        tv_date = findViewById(R.id.tv_date);
        tv_name = findViewById(R.id.user);

        dayTaskBar = (ProgressBar)findViewById(R.id.progressBar1);
        historyTaskBar = (ProgressBar)findViewById(R.id.progressBar2);



        changePsw = (Button)findViewById(R.id.change_psw);
        changePsw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getBaseContext(),ModifyPasswordActivity.class);
                startActivity(intent);
            }
        });

        //进入不到登录界面
        logout = (Button)findViewById(R.id.logout);
        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(PersonInfoActivity.this,LoginActivity.class)
                        .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                SPUtil.putData(PersonInfoActivity.this,SPUtil.KEY_LOGIN,false);
                SPUtil.putData(PersonInfoActivity.this,SPUtil.KEY_ID,"");

                // 关闭提醒服务
                Intent serviceIntent = new Intent(PersonInfoActivity.this, TimeTaskService.class);
                stopService(serviceIntent);

                finish();
            }
        });

        cancel = (ImageView)findViewById(R.id.cl_cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PersonInfoActivity.this.finish();
            }
        });

        initData();


    }

    private void initData() {
        userId = SPUtil.getUserId(this);
        dbHelper = new DatabaseHelper(this);

        tv_date.setText(Util.getDayNowShow());
        tv_name.setText(dbHelper.getUserNameById(Integer.parseInt(userId)));

        UserBean.DataBean user = dbHelper.getUserData(Integer.parseInt(userId)).getData();

        Log.e("TAG", "initData: "+user.toString() );
        dayTask.setText(user.getTodayDoneTaskNum() + "/" + user.getTodayTaskNum());

        float percentHistory = 0;
        if(user.getHistoryTaskNum()!=0){
            percentHistory =  ((float) user.getHistoryDoneTaskNum()/user.getHistoryTaskNum())*100;
        }

        historyTask.setText(percentHistory + "%");
        finshedTask.setText(user.getHistoryDoneTaskNum()+"");
        waitedTask.setText(user.getWaitTaskNum()+"");
        date.setText(user.getCreateTime()+"");


        float percentToday = 0;
        if(user.getTodayTaskNum()!=0){
            percentHistory =  (float)user.getTodayDoneTaskNum()/user.getTodayTaskNum()*100;
        }
        dayTaskBar.setProgress((int)percentToday);
        historyTaskBar.setProgress((int)percentHistory);

    }
}